package cn.bytecollege.controller;

import cn.bytecollege.mapper.UserAddressMapper;
import cn.bytecollege.model.Goods;
import cn.bytecollege.model.OrderDetail;
import cn.bytecollege.model.Orders;
import cn.bytecollege.model.UserAddress;
import cn.bytecollege.service.CommunityService;
import cn.bytecollege.service.FileUploadService;
import cn.bytecollege.vo.GoodsVO;
import cn.hutool.crypto.Mode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.rmi.MarshalledObject;
import java.util.Date;
import java.util.List;
@Log
@Controller
@RequestMapping("goods")
public class CommunityController {

    String PATH = "/src/main/resources/static/images/goodsImage";
    @Autowired
    private CommunityService communityService;
    @Autowired
    private FileUploadService fileUploadService;


    /**
     * 查询物资所有信息
     * @param model
     * @return
     */
    @GetMapping("getAll")
    public String getAll(Model model){
//        List<Goods> list = communityService.getAll();
//        model.addAttribute("list",list);
//        log.info(list.toString());
        return "admin/index";
    }

    /**
     * 转发到添加物资页面
     * @return
     */
    @GetMapping("add")
    public String add(){
        return "admin/addGoods";
    }

    /**
     * 处理数据
     * @param goodsVO
     * @return
     */
    @PostMapping("/add")
    public String add(GoodsVO goodsVO, MultipartFile file){
        String imagePath = fileUploadService.fileUploadGoodsImages(PATH, file);
        goodsVO.setGoodsImage(imagePath);
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVO,goods);
        communityService.add(goods);
        if(goods.getGoodsKind().equals("食品")){
            return "redirect:/goods/food";
        }else if(goods.getGoodsKind().equals("生活用品")){
            return "redirect:/goods/life";
        }
        return "redirect:/goods/cloth";
    }

    /**
     * 删除物资
     * @param id
     * @return
     */
    @GetMapping("delGoods")
    public String delById(Integer id){
        log.info(id.toString());
        communityService.delById(id);
        return "redirect:/goods/food";
    }

    /**
     * 修改物资
     * @param model
     * @param id
     * @return
     */
    @GetMapping("modGoods")
    public String modById(Model model,Integer id){
        Goods goods = communityService.getById(id);
        log.info(id.toString());
        model.addAttribute("goods",goods);
        return "admin/modGoods";
    }
    @PostMapping("modGoods")
    public String modById(GoodsVO goodsVO){
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVO,goods);
        communityService.modById(goods);
        if(goods.getGoodsKind().equals("食品")){
            return "redirect:/goods/food";
        }else if(goods.getGoodsKind().equals("生活用品")){
            return "redirect:/goods/life";
        }
        return "redirect:/goods/cloth";
    }

    /**
     * 查询类型为事务的物资
     * @param model
     * @param pn
     * @return
     */
    @GetMapping("food")
    public String foodList(Model model,@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn){
        Page<Goods> page = communityService.foodList(pn,5);
        model.addAttribute("jumpUrl","/goods/food?pn=");
        List<Goods> list = page.getRecords();
        model.addAttribute("page",page);
        model.addAttribute("list",list);
        return "admin/goodsList";
    }

    /**
     * 查询类型为生活用品的物资
     * @param model
     * @param pn
     * @return
     */
    @GetMapping("life")
    public String lifeList(Model model,@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn){
        Page<Goods> page = communityService.lifeList(pn,5);
        model.addAttribute("jumpUrl","/goods/life?pn=");
        List<Goods> list = page.getRecords();
        model.addAttribute("page",page);
        model.addAttribute("list",list);
        return "admin/goodsList";
    }

    /**
     * 查询类型为衣物的物资
     * @param model
     * @param pn
     * @return
     */
    @GetMapping("cloth")
    public String clothList(Model model,@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn){
        Page<Goods> page = communityService.clothList(pn,5);
        model.addAttribute("jumpUrl","/goods/cloth?pn=");
        List<Goods> list = page.getRecords();
        model.addAttribute("page",page);
        model.addAttribute("list",list);
        return "admin/goodsList";
    }

    /**
     * 查询全部订单
     * @param model
     * @param pn
     * @return
     */
    @GetMapping("orderList")
    public String orderList(Model model,@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn){
        Page<Orders> page = communityService.ordersList(pn,5);
        model.addAttribute("jumpUrl","/goods/orderList?pn=");
        List<Orders> list = page.getRecords();
        model.addAttribute("page",page);
        model.addAttribute("orders",list);
        return "admin/orderList";
    }

    /**
     * 根据日期查询订单
     * @param orderDate
     * @param model
     * @return
     */

    @GetMapping("orders")
    public String ordersList(@DateTimeFormat(pattern="yyyy-MM-dd")Date orderDate, Model model,@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn){
        Page<Orders> page = communityService.ordersList(orderDate,pn,2);
        List<Orders> orders = page.getRecords();
        model.addAttribute("orders",orders);
        model.addAttribute("jumpUrl","/goods/orders?pn=");
        model.addAttribute("page",page);
        return "admin/orderList";
    }

    /**
     * 查询状态为待揽件的订单
     * @param model
     * @param pn
     * @return
     */
    @GetMapping("addTask")
    public String addTask(Model model,@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn){
        Page<Orders> page = communityService.addTask(pn,5);
        List<Orders> orders = page.getRecords();
        log.info(orders.toString());
        model.addAttribute("orders",orders);
        model.addAttribute("page",page);
        model.addAttribute("jumpUrl","/goods/orders?pn=");
        return "admin/addTask";
    }

    /**
     * 发布订单
     * @param id
     * @return
     */
   @GetMapping("addOrder")
    public String addTask(Integer id){
        Orders orders = new Orders();
        orders.setOrderId(id);
        orders.setOrderState("待揽件");
        log.info(orders.toString());
        communityService.addOrder(orders);
        return "redirect:/goods/orderList";
    }

    /**
     * 根据名称搜索物资
     * @param goodsName
     * @param model
     * @param pn
     * @return
     */
    @PostMapping("getByName")
    public String getByName(String goodsName,Model model,@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn){
        Page<Goods> page = communityService.getByPage(goodsName, pn, 5);
        List<Goods> list = page.getRecords();
        model.addAttribute("page",page);
        model.addAttribute("list",list);
        model.addAttribute("jumpUrl","/goods/orders?pn=");
        return "admin/goodsList";
    }
    @GetMapping("commC")
    public String commC(){
        return "system/communityWorkerList";
    }
    @GetMapping("commD")
    public String commD(){
        return "system/distributionWorkerList";
    }
    @GetMapping("setup")
    public String setup(){
        return "admin/setup";
    }
    @GetMapping("agree")
    public String agree(Integer id){
        System.out.println(id);
        Orders orders = communityService.getByOrders(id);
        String orderState = orders.getOrderState();
        System.out.println(orders);
        if(orderState.equals("待揽件")||orderState.equals("待发货")){
            orders.setOrderCheck("已通过");
            orders.setOrderState("已取消");
        }
        System.out.println(orders);
        communityService.modByOrderId(orders);
        log.info(orders.toString());
        return "redirect:/goods/orderList";
    }
    @GetMapping("refuse")
    public String refuse(Integer id){
        System.out.println(id);
        Orders orders = communityService.getByOrders(id);
        String orderState = orders.getOrderState();
        System.out.println(orders);
        if(!(orderState.equals("待揽件")&&orderState.equals("待发货"))){
            orders.setOrderCheck("已拒绝");
        }
        System.out.println(orders);
        communityService.modByOrderId(orders);
        log.info(orders.toString());
        return "redirect:/goods/orderList";
    }
}
